package com.snoob.serviceEnterprise.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snoob.serviceEnterprise.enterprise.entity.EnterpriseUserEntity;
import com.snoob.serviceEnterprise.enterprise.mapper.EnterpriseUserMapper;
import com.snoob.serviceEnterprise.enterprise.service.EnterpriseUserService;
import com.snoob.springcloudalibaba.base.BaseException;
import com.snoob.springcloudalibaba.pipleline.qo.SelectEnterpriseUserQo;
import com.snoob.springcloudalibaba.pipleline.vo.SelectEnterpriseUserByPageVoRes;
import com.snoob.springcloudalibaba.util.StrUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 企业用户 服务实现类
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2022-05-14
 */
@Service
public class EnterpriseUserServiceImpl extends ServiceImpl<EnterpriseUserMapper, EnterpriseUserEntity> implements EnterpriseUserService {
    @Resource
    private EnterpriseUserMapper enterpriseUserMapper;

    /**
     * @描述 添加企业用户信息
     */
    @Override
    public String insertEnterpriseUser(EnterpriseUserEntity entity) {
        // 非空校验
        if (null == entity) {
            throw new BaseException("参数缺失");
        }
        // 唯一性校验 ....
        // 添加
        entity.setCreateTime(new Date());
        entity.setUserPassword(UUID.randomUUID().toString()); // 随机生成密码
        boolean enterpriseUserEntityFlag = this.save(entity);
        return enterpriseUserEntityFlag ? entity.getUserAccount() : null;
    }

    /**
     * @描述 查询企业用户信息
     */
    @Override
    public EnterpriseUserEntity selectEnterpriseUser(SelectEnterpriseUserQo qo) {
        // 非空校验
        if (null == qo) {
            throw new BaseException("参数缺失");
        }
        if (!StrUtil.isEffective(qo.getUserAccount())) {
            throw new BaseException("企业用户账号缺失");
        }
        // 查询条件
        QueryWrapper<EnterpriseUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account", qo.getUserAccount());
        EnterpriseUserEntity enterpriseUserEntity = this.getOne(queryWrapper);
        return enterpriseUserEntity;
    }

    /**
     * @描述 分页查询企业用户信息
     */
    @Override
    public Page<SelectEnterpriseUserByPageVoRes> selectEnterpriseUserByPage(SelectEnterpriseUserQo qo) {
        // 非空校验
        if (!StrUtil.isEffective(qo.getEnterpriseCode())) {
            throw new BaseException("参数缺失");
        }
        // 分页查询
        Page<SelectEnterpriseUserByPageVoRes> page = new Page<SelectEnterpriseUserByPageVoRes>(qo.getBasePageNum(), qo.getBasePageSize());
        page = enterpriseUserMapper.selectEnterpriseUserByPage(page, qo);
        return page;
    }
}
